/*
 * Implemento la tecnologia de ray traking.
 *
 * This is part of the game The Rooms
 * Copyright (C) 2010  Elvio Sarquis
 *
 * Get the GNU GPL v3 in http://www.gnu.org/licenses/
 */
package utils;

import java.util.Date;
import java.util.Random;

/**
 *
 * @author dask
 */
public class Logic {

    private static long oldseed = 0;

    public static int rand(int max, int min) {

        Date now = new Date();
        long seed = now.getTime() + oldseed;

        oldseed = seed;

        Random randomizer = new Random(seed);

        int n = max - min + 1;

        if (n < 0) n = -n;

        int i = randomizer.nextInt(n);

        if (i < 0) i = -i;

        return min + i;
    }

    public static int abs(int n){

        if (n<0) n = -n;
        return n;
    }

    public static Point[] getRay(int x0, int y0, int x1, int y1, int range) {

        Point[] points = new Point[0];
        Point newone = new Point(x0, y0);



        int w = x1 - x0;


        int h = y1 - y0;


        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;


        int longest = Math.abs(w);


        int shortest = Math.abs(h);


        if (w < 0) {
            dx1 = -1;


        } else if (w > 0) {
            dx1 = 1;


        }
        if (h < 0) {
            dy1 = -1;


        } else if (h > 0) {
            dy1 = 1;


        }
        if (w < 0) {
            dx2 = -1;


        } else if (w > 0) {
            dx2 = 1;


        }

        if (!(longest > shortest)) {
            longest = Math.abs(h);
            shortest = Math.abs(w);


            if (h < 0) {
                dy2 = -1;


            } else if (h > 0) {
                dy2 = 1;


            }
            dx2 = 0;


        }
        int numerator = longest >> 1;


        for (int i = 0; i
                <= longest; i++) {

            newone.set(x0, y0);
            points = addPoint(newone, points);

            numerator += shortest;


            if (!(numerator < longest)) {
                numerator -= longest;
                x0 += dx1;
                y0 += dy1;


            } else {
                x0 += dx2;
                y0 += dy2;


            }

            if (range != 0 && range == i) {
                break;
            }


        }

        return points;


    }

    public static Point[] getRay(int x0, int y0, int x1, int y1) {

        return getRay(x0, y0, x1, y1, 0);


    }

    private static Point[] addPoint(Point point, Point[] arre) {

        Point[] tempo = new Point[arre.length + 1];



        for (int i = 0; i
                < arre.length; i++) {

            tempo[i] = arre[i];


        }


        tempo[arre.length] = new Point();
        tempo[arre.length].x(point.x());
        tempo[arre.length].y(point.y());



        return tempo;


    }
}
